﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Text;

namespace lcs.Web.supply
{
    public partial class ListTechnical : System.Web.UI.Page
    {
        Maticsoft.BLL.supplyPlan sm = new Maticsoft.BLL.supplyPlan();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    Bind(user_id);
                }
                //Bind(11);
            }
        }

        private void Bind(int user_id)
        {
            string str = "  user_id= " + user_id + " and innums < num and technicalDataStatus = '未确认' ";//已入库数量 < 计划数量或者已入库数量为null时

            if (!string.IsNullOrEmpty(txt_material.Text))
            {
                str += " and material like '%"+txt_material.Text+"%' ";
            }
            if (!string.IsNullOrEmpty(txt_projectName.Text))
            {
                str += " and projectName like '%"+txt_projectName.Text+"%' ";
            }

            if (!string.IsNullOrEmpty(txt_supplier.Text))
            {
                str += " and sname like '%" + txt_supplier.Text + "%' ";
            }


            DataSet ds = sm.GetListSumInnumsInfo(str);

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();

            this.lbl_count.Text = " 当前共有 "+ds.Tables[0].Rows.Count+" 条记录";
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_add")
            {
                if (e.CommandArgument != null)
                {
                    //string supplyPlanId = e.CommandArgument.ToString();

                    //Response.Redirect("AddMonthSupply.aspx?supplyPlanId="+supplyPlanId);

                    Session["supplyPlanId"] = e.CommandArgument.ToString();

                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPop('FramePopTechnical.aspx','900px','390px');</script>");
                }
               
            }
            if (e.CommandName == "outconfirm")
            {
                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string unit, proname, id, jdate, jhnum, wl, supplier;//需用单位，项目名称，采购订单号，合同交货期，招标批次，货物名称,供应商名称
                //int pid = int.Parse(gwSearch.DataKeys[gvr.RowIndex].Value.ToString());    
                jhnum = gvr.Cells[17].Text.Trim();//中标批次
                wl = gvr.Cells[6].Text.Trim();//物料描述
                id = gvr.Cells[18].Text.Trim();//采购订单号
                //unit = gvr.Cells[11].Text.Trim();
                unit = gvr.Cells[2].Text.Trim();//主送和供应商回复中经办人都改为了供应商名称
                jdate = gvr.Cells[3].Text.Trim();//供货期
                proname = gvr.Cells[4].Text.Trim();//项目名称
                supplier = gvr.Cells[2].Text.Trim();//供应商名称
                //name = ((Label)gvr.Cells[25].Controls[1]).Text;
                //fax = ((Label)gvr.Cells[26].Controls[1]).Text;
                string file = ExWord.ExportToWordconfirm(unit, jdate, jhnum, proname, id, wl);
                FileInfo DownloadFile = new FileInfo(file);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();

            }
            if (e.CommandName == "outex")
            {
                //int index = int.Parse(e.CommandArgument.ToString());
                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string unit, proname, id, jdate, jhnum, wl, supplier;
                jhnum = gvr.Cells[17].Text.Trim();//中标批次
                wl = gvr.Cells[6].Text.Trim();//物料描述
                id = gvr.Cells[18].Text.Trim();//采购订单号
                //unit = gvr.Cells[11].Text.Trim();
                unit = gvr.Cells[2].Text.Trim();//主送和供应商回复中经办人都改为了供应商名称
                jdate = gvr.Cells[3].Text.Trim();//供货期
                proname = gvr.Cells[4].Text.Trim();//项目名称
                supplier = gvr.Cells[2].Text.Trim();//供应商名称
                string file = ExWord.ExportToWordExped(unit, jdate, jhnum, proname, id, wl);
                FileInfo DownloadFile = new FileInfo(file);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();

            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }

        }

        protected void btn_reload_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }
        }
        //查询
        protected void btn_sel_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }
        }

        protected void btn_confirm_Click(object sender, EventArgs e)
        {
            string technicalDataStatusDate = this.txt_technicalDataStatusDate.Text;

            if (!string.IsNullOrEmpty(technicalDataStatusDate))
            {
                for (int i = 0; i < this.GridView1.Rows.Count; i++)
                {
                    CheckBox cbo = this.GridView1.Rows[i].FindControl("cbo_check") as CheckBox;

                    if (cbo != null && cbo.Checked)
                    {
                        Label lbl = this.GridView1.Rows[i].FindControl("lbl_supplyPlanId") as Label;

                        if (!string.IsNullOrEmpty(lbl.Text))
                        {
                            string supplyPlanId = lbl.Text;

                            Maticsoft.Model.supplyPlan s = sm.GetModel(int.Parse(supplyPlanId));

                            s.technicalDataStatus = "已确认";
                            s.technicalDataStatusDate = DateTime.Parse(technicalDataStatusDate);

                            sm.Update(s);
                        }
                    }

                }

                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    Bind(user_id);
                }

            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "请确认日期！");
            }

          
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }

    }
}